package com.lakeworks.mapper;

import com.lakeworks.domain.OprProduct;
import com.lakeworks.domain.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * 作业产品Mapper接口
 *
 * @author lake
 * @date 2023-02-08
 */
public interface OprProductMapper {
    OprProduct queryByBarcode(@Param("barcode") String barcode);

    OprProduct queryByBarcodeAndStatus(@Param("barcode") String barcode, @Param("status") String status);

    /**
     * 查询作业产品
     *
     * @param id 作业产品主键
     * @return 作业产品
     */
    OprProduct selectOprProductById(Long id);

    OprProduct queryByCompanionId(@Param("companionId") Long companionId);

    /**
     * 查询作业产品列表
     *
     * @param oprProduct 作业产品
     * @return 作业产品集合
     */
    List<OprProduct> selectOprProductList(OprProduct oprProduct);


    List<OprProduct> selectProductList(OprProduct oprProduct);

    /**
     * 新增作业产品
     *
     * @param oprProduct 作业产品
     * @return 结果
     */
    int insertOprProduct(OprProduct oprProduct);

    /**
     * 修改作业产品
     *
     * @param oprProduct 作业产品
     * @return 结果
     */
    int updateOprProduct(OprProduct oprProduct);


    int updateProductTowash(OprProduct oprProduct);



    int updateByIdOrCompanionId(@Param("oprProduct") OprProduct oprProduct);

    int updateByProductIdOrCompanionId(@Param("oprProduct") OprProduct oprProduct);

    int updateByCompanionId(@Param("oprProduct") OprProduct oprProduct);

    /**
     * 删除作业产品
     *
     * @param id 作业产品主键
     * @return 结果
     */
    int deleteOprProductById(Long id);

    /**
     * 批量删除作业产品
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    //int deleteOprProductByIds(String[] ids);

    int deleteFlagByIds(OprProduct oprProduct);


    int insertProductVo(ProductVo productVo);

    int updateProductVo(ProductVo productVo);

    ProductVo selectOprProductByBarcode(OprProduct oprProduct);

    int updateStatueById(OprProduct oprProduct);

    int updateProductStatusById(OprProduct oprProduct);

    List<OprProduct> queryByPickupId(@Param("pickupId") Long pickupId);

    List<OprProduct> queryByShipmentId(@Param("shipmentId") Long shipmentId);

    List<OprProduct> queryByShipmentIdExcludeCompanion(@Param("shipmentId") Long shipmentId);

    @Select("select a.id,a.channel_id channelId,a.pickup_id pickupId,a.product_id productId,a.companion_id companionId,a.merchandise_id merchandiseId,a.shipment_id shipmentId,a.barcode,a.frame_no frameNo,a.status,a.comment,(select b.name from sys_product b where b.id = a.product_id) as name,flag from opr_product a where a.merchandise_id = #{merchandiseId} and a.flag & 512 != 512 and a.companion_id is null")
    ProductVo selectProductVoInfo(@Param("merchandiseId") long merchandiseId);

    List<OprProduct> selectProductByShipmentId(Long id);

    @Select("select opt.channel_id channelId,opt.id productId,\n" +
            "opt.pickup_id pickupId,opt.shipment_Id shipmentId,\n" +
            "opt.barcode,opt.frame_no frameNo,\n" +
            "spt.name productType,\n" +
            "opp.order_no orderNo,\n" +
            "SUBSTRING_INDEX(opp.comment,'@=@', 1) 'userComment',\n" +
            "SUBSTRING_INDEX(opp.comment,'@=@', -1) 'channelComment'\n" +
            "from opr_product opt\n" +
            "left join sys_product spt\n" +
            "on opt.product_id = spt.id\n" +
            "left join opr_pickup opp\n" +
            "on opt.pickup_id = opp.id\n" +
            "where opt.id = #{productId}\n" +
            "and opt.flag & 512 != 512")
    CheckProductVO queryCheckProVOById(@Param("productId") Long productId);

    OprProduct selectOprProductByCompanionId(Long id);

    ProductVo selectProductVoById(Long id);

    List<OprProduct> selectProductByOrderNo(String orderNo);


    List<Long> selectTimeOutProduct(Integer days);

    List<OprProduct> selectIndexProductList(OprProduct oprProduct);


    int deleteFlagByBarcode(OprProduct oprProduct);


    int updateProductShipmentId(String barcode);


    List<OprProduct> selectBarcodeByOrderNo(String orderNo);

    List<OprProduct> getByBarcodeAndStatus(@Param("status") String status,@Param("para") PackageQueryPara para);

    int getCountByBarcodeAndStatus(String status, PackageQueryPara para);

    @Select("SELECT opt.id,opt.barcode,spt.name,spt.category \n" +
            "FROM opr_product opt\n" +
            "LEFT JOIN sys_product spt \n" +
            "ON opt.product_id = spt.id\n" +
            "WHERE opt.shipment_id = #{shipmentId}\n" +
            "AND opt.flag & 512 != 512\n" +
            "AND opt.companion_id IS NULL ")
    List<OprSysProduct> oprSysProductByShipmentId(@Param("shipmentId") Long shipmentId);



    List<OprProduct> selectClothList(ClothInfo clothInfo);

    @Update("update opr_product set flag = 512 where pickup_id = #{pickupId}")
    int pickupCancel(@Param("pickupId") Long pickupId);


    OprProduct selectNoQCByBarcode(String barcode);


    OprProduct selectProductByBarcodeAndStatus(OprProduct oprProduct);

    List<Map<String,Object>> selectLeaveFactoryList(Long channelId,String washMode,String startTime, String endTime);

    OprProduct selectOprProductByMerchandiseId(Long merchandiseId);

    List<Long> releasableFrame(@Param("frameNo") String frameNo);

    Integer selectProductCount(@Param("state") String state,@Param("hour") Integer hour);
    int updateProductInfo(ProductVo productVo);

    Integer selectNoAttchProductCount(Integer hour);

    Integer selectAttchProductCount(Integer hour);


}
